#include <GL/glew.h>
#include "SFML/Graphics.hpp"
#include "FreeImage.h"
#include <iostream>


using namespace std;

#ifndef __GENERIC_IMAGE_HPP__
#define __GENERIC_IMAGE_HPP__

void createImage(int width_, int height_, char* data_, string filename); 


class GenericImage {
		unsigned char* m_imageData;
		unsigned int m_width, m_height;
		GLuint m_linkedTextureId;
	public:
		/** constructor of a GenericImage loading image 
		  * from file filename */
		GenericImage(string filename) {
			m_linkedTextureId = 0;
			m_imageData = NULL;
			loadImageFromFile(filename);
			//std::cout << "load error ? : " << loadImageFromFile(filename) << std::endl;
		};

		/** load an image from file filename
		 *	return int an error code (0 : error, 1 : all good)
		 */
		int loadImageFromFile(string filename);

		/** using this GenericImage to define a texture that will be
		  * bound to wantedId if positive or to a new id return as 
		  * function result */
		GLuint defineTextureFromImage(int wantedId = -1);

		/** function for retrieving GenericImage width */
		unsigned int getWidth() { return m_width;};

		/** function for retrieving GenericImage height */
		unsigned int getHeight() { return m_height;};

		/** function for retrieving GenericImage data */
		const unsigned char* getData() { return m_imageData;}

		/** debug function for printing GenericImage data */
		void printImg() {
			cout << "width : " << m_width << " height : " << m_height << endl;
			for (unsigned int x = 0; x < m_width; x++) {
				for (unsigned int y = 0; y < m_height; y++) {
					cout << " |" << hex << m_imageData[(x*m_height+y)*4 + 0] << "-"
								 << m_imageData[(x*m_height+y)*4 + 1] << "-"
								 << m_imageData[(x*m_height+y)*4 + 2] << "-"
								 << m_imageData[(x*m_height+y)*4 + 3] << "|";
				};
				cout << endl;
			};

		};
};

#endif
